// 当前时间添加几天
export const addDayFormat = (date: any, num: number,fmt: string='YYYY-mm-dd HH:MM:SS') => {
  if (typeof date === "string" || typeof date === "number") {
    date = new Date(date);
  }

  const _time = date.setDate(parseInt(date.getDate()) + Number(num));
  return dateFormat(_time, fmt) 
};
/**
 * 月的最后一天
 * @param date
 * @returns
 */
export const getCurrentMonthLast = (date: any) => {
  var endDate = new Date(date); // date 是需要传递的时间如：2018-08
  var month = endDate.getMonth();
  var nextMonth = ++month;
  var nextMonthFirstDay = new Date(endDate.getFullYear(), nextMonth, 1);
  var oneDay = 1000 * 60 * 60 * 24;
  var dateString = new Date(Number(nextMonthFirstDay) - oneDay);
  return dateString.toLocaleDateString().replace(new RegExp("/", "g"), "-"); // toLocaleDateString() 返回 如：2018/8/31
};
/**
 * 格式化为年-月
 * @param {*} date 时间
 */
export const normalMonthFormat = (date: any) => {
  if (typeof date === "string") {
    date = new Date(date);
  }
  return dateFormat(date, "YYYY-mm");
};
/**
 * 格式化为年-月-日
 * @param {*} date 时间
 */
export const normalDayFormat = (date: any,fmt:string='YYYY-mm-dd') => {
  if (typeof date === "string") {
    date = new Date(date);
  }
  return dateFormat(date, fmt);
};
/**
 * 格式化为时分秒
 * @param {*} date 时间
 */
export const normalTimeFormat = (date: any) => {
  if (typeof date === "string") {
    date = new Date(date);
  }
  return dateFormat(date, "HH:MM:SS");
};
/**
 * 格式化为年-月-日 时分秒
 * @param {*} date 时间
 */
export const normalDateFormat = (date: any) => {
  if (typeof date === "string") {
    date = new Date(date);
  }
  return dateFormat(date, "YYYY-mm-dd HH:MM:SS");
};
/**
 * 格式化为年-月-日 时分
 * @param {*} date 时间
 */
export const normalMinuteFormat = (date: any) => {
  if (typeof date === "string") {
    date = new Date(date);
  }
  return dateFormat(date, "YYYY-mm-dd HH:MM");
};
/**
 * 格式化为时分
 * @param {*} date 时间
 */
export const normalHourMinuteFormat = (date: any) => {
  if (typeof date === "string") {
    date = new Date(date);
  }
  return dateFormat(date, "HH:MM");
};
/**
 * 格式化时间
 * @param {*} date 时间
 * @param {*} fmt 'YYYY-mm-dd HH:mm:ss'
 */
const dateFormat = (date: any, fmt: string) => {
  if (
    !date ||
    date == null ||
    date == undefined ||
    date === "undefined" ||
    date === "null" ||
    date === ""
  ) {
    return "/";
  }
  if (typeof date === "string" || typeof date === "number") {
    date = new Date(date);
  }
  let ret;
  const opt: any = {
    "Y+": date.getFullYear().toString(), // 年
    "m+": (date.getMonth() + 1).toString(), // 月
    "d+": date.getDate().toString(), // 日
    "H+": date.getHours().toString(), // 时
    "M+": date.getMinutes().toString(), // 分
    "S+": date.getSeconds().toString(), // 秒
    // 有其他格式化字符需求可以继续添加，必须转化成字符串
  };
  for (const k in opt) {
    ret = new RegExp("(" + k + ")").exec(fmt);
    if (ret) {
      fmt = fmt.replace(
        ret[1],
        ret[1].length === 1 ? opt[k] : opt[k].padStart(ret[1].length, "0")
      );
    }
  }
  return fmt;
};

// 一年的第一秒
export const firstTimeByYear = () => {
  return new Date().getFullYear() + "-01-01 00:00:00";
};

/**
 * 金额数字格式化
 * @param number
 * @param places
 * @param symbol
 * @param thousand
 * @param decimal
 * @returns
 */
export const formateMoney = (
  number: any,
  places: any = "2",
  symbol: any = "$",
  thousand: any = ",",
  decimal: any = "."
) => {
  number = number || 0;
  places = !isNaN((places = Math.abs(places))) ? places : 2;
  symbol = symbol !== undefined ? symbol : "$";
  thousand = thousand || ",";
  decimal = decimal || ".";
  var negative = number < 0 ? "-" : "";
  var i: any =
    parseInt((number = Math.abs(+number || 0).toFixed(places)), 10) + "";
  var j: any = (j = i.length) > 3 ? j % 3 : 0;
  return (
    symbol +
    negative +
    (j ? i.substr(0, j) + thousand : "") +
    i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thousand) +
    (places
      ? decimal +
        Math.abs(number - i)
          .toFixed(places)
          .slice(2)
      : "")
  );
};
/**
 * 人命币格式化
 * @param number
 * @returns
 */
export const formatRMB = (number: any) => {
  if (!number || number == null || number == "") number = 0;
  return formateMoney(number, 2, "¥");
};

/**
 * 文件名时间戳
 * @returns
 */
export const fileNameTimeFormat = () => {
  const _now = dateFormat(new Date(), "YYYY mm dd HH MM SS").replace(
    /\s*/g,
    ""
  );
  return _now;
};
